package com.yc.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.study.entity.Job;
import com.yc.study.entity.JobTpoic;
import com.yc.study.entity.User;
import com.yc.study.exception.BusinessException;
import com.yc.study.mapper.JobMapper;
import com.yc.study.mapper.UserMapper;
import com.yc.study.service.IJobService;
import com.yc.study.service.IJobTpoicService;
import com.yc.study.vo.JobVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author 王妍
 * @since 2022-02-20
 */
@Service
@AllArgsConstructor
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements IJobService {
  private final IJobTpoicService jobTpoicService;

  private final JobMapper jobMapper;

  private final UserMapper userMapper;

  /**
   * 老师布置作业
   *
   * @param teacherName 老师名字
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void saveJob(JobVo jobVo, String teacherName) {
    Job job = new Job();
    BeanUtils.copyProperties(jobVo, job);
    final Long teacherId =
        Optional.ofNullable(
                userMapper.selectOne(
                    new LambdaQueryWrapper<User>().eq(User::getUsername, teacherName)))
            .map(User::getId)
            .orElseThrow(() -> new BusinessException("用户不存在！！！"));
    job.setTeacherId(teacherId);
    saveOrUpdate(job);
    // 清除历史数据
    jobTpoicService.remove(new LambdaQueryWrapper<JobTpoic>().eq(JobTpoic::getJobId, job.getId()));
    // 保存新数据
    List<JobTpoic> jobTopicList =
        Arrays.stream(jobVo.getTopicIds().split(","))
            .map(Long::parseLong)
            .map(
                topicId -> {
                  JobTpoic jobTpoic = new JobTpoic();
                  jobTpoic.setJobId(job.getId());
                  jobTpoic.setTopicId(topicId);
                  return jobTpoic;
                })
            .collect(Collectors.toList());
    jobTpoicService.saveBatch(jobTopicList);
  }

  @Override
  public Page<JobVo> loadPageVoByCourseId(Page<JobVo> jobVoPage, Long courseId) {
    return jobMapper.loadPageVoByCourseId(jobVoPage, courseId);
  }

  @Override
  public Page<JobVo> loadPageVoByTeacherId(Page<JobVo> page, JobVo jobVo) {
    return jobMapper.loadPageVoByTeacherId(page, jobVo);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void deleteJobs(List<Long> asList) {
    // 1.删除主表
    jobMapper.deleteBatchIds(asList);
    // 2。删除关联表
    jobTpoicService.remove(new LambdaQueryWrapper<JobTpoic>().in(JobTpoic::getJobId, asList));
  }

  @Override
  public Page<JobVo> loadPageVoByStudentId(Page<JobVo> jobVoPage, Long id) {
    return jobMapper.selectPageVoByStudentId(jobVoPage, id);
  }
}
